package solution;

/**
 * <a href="https://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec?tpId=188&&tqId=38556&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking">
 *     NC7 买卖股票的最好时机</a>
 * @author or2
 * @date 2021/7/31 23:14
 */
public class MaxProfit {

    /**
     *
     * @param prices int整型一维数组
     * @return int整型
     */
    public int maxProfit (int[] prices) {
        int minCurrent = prices[0];
        int maxPro = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            int priceNext = prices[i + 1];

            if (prices[i] < priceNext) {
                continue;
            }

            int temp = prices[i] - minCurrent;
            if (temp > maxPro) {
                maxPro = temp;
            }

            if (priceNext < minCurrent) {
                minCurrent = priceNext;
            }
        }

        return Math.max(prices[prices.length - 1] - minCurrent, maxPro);
    }
}
